home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / cool.lha / ice / pisces / byacc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-04  |  6.7 KB  |  361 lines

  1.  
  2. /**
  3.  ** MODIFIED: 06/12/90 by MBN
  4.  **
  5.  ** As originally written, byacc assumed that pathnames followed UNIX syntax,
  6.  ** which is not appropriate for a truly portable tool. The symbols `os2' and
  7.  ** `vms' have been added to handle file name parsing correctly for those
  8.  ** platforms. Also added the character pathsep that contains the character
  9.  ** used as a pathname separator. Default for both is UNIX-style names.
  10.  **/
  11.  
  12. /**
  13.  ** MODIFIED: 05/12/90 by MBN
  14.  **
  15.  ** Changed default files names from y.tab.[hc] to y_tab.[hc]. Also changed the
  16.  ** verbose file from y.output to y_out.txt. These file names are more portable
  17.  ** to other operating systems such as VMS and OS/2. Note the the "y" prefix
  18.  ** can be modified by the command line option -bprefix.
  19.  **/
  20.  
  21. #include <signal.h>
  22. #include "defs.h"
  23.  
  24. char dflag;
  25. char lflag;
  26. char tflag;
  27. char vflag;
  28.  
  29. char *prefix = "y";
  30. char *myname = "yacc";
  31. char *temp_form = "yacc.XXX";
  32.  
  33. char pathsep = '/';
  34.  
  35. int lineno;
  36. int outline;
  37.  
  38.  
  39. char *action_file_name;
  40. char *defines_file_name;
  41. char *input_file_name = "";
  42. char *output_file_name;
  43. char *text_file_name;
  44. char *union_file_name;
  45. char *verbose_file_name;
  46.  
  47. FILE *action_file;    /*  a temp file, used to save actions associated    */
  48.             /*  with rules until the parser is written        */
  49. FILE *defines_file;    /*  y_tab.h                        */
  50. FILE *input_file;    /*  the input file                    */
  51. FILE *output_file;    /*  y_tab.c                        */
  52. FILE *text_file;    /*  a temp file, used to save text until all        */
  53.             /*  symbols have been defined                */
  54. FILE *union_file;    /*  a temp file, used to save the union            */
  55.             /*  definition until all symbol have been        */
  56.             /*  defined                        */
  57. FILE *verbose_file;    /*  y_out.txt                        */
  58.  
  59. int nitems;
  60. int nrules;
  61. int nsyms;
  62. int ntokens;
  63. int nvars;
  64.  
  65. int   start_symbol;
  66. char  **symbol_name;
  67. short *symbol_value;
  68. short *symbol_prec;
  69. char  *symbol_assoc;
  70.  
  71. short *ritem;
  72. short *rlhs;
  73. short *rrhs;
  74. short *rprec;
  75. char  *rassoc;
  76. short **derives;
  77. char *nullable;
  78.  
  79. extern char *mktemp();
  80. extern char *getenv();
  81.  
  82.  
  83. done(k)
  84. int k;
  85. {
  86.     if (action_file) { fclose(action_file); unlink(action_file_name); }
  87.     if (text_file) { fclose(text_file); unlink(text_file_name); }
  88.     if (union_file) { fclose(union_file); unlink(union_file_name); }
  89.     exit(k);
  90. }
  91.  
  92.  
  93. onintr()
  94. {
  95.     done(1);
  96. }
  97.  
  98.  
  99. set_signals()
  100. {
  101. #ifdef SIGINT
  102.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  103.     signal(SIGINT, onintr);
  104. #endif
  105. #ifdef SIGTERM
  106.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  107.     signal(SIGTERM, onintr);
  108. #endif
  109. #ifdef SIGHUP
  110.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  111.     signal(SIGHUP, onintr);
  112. #endif
  113. }
  114.  
  115.  
  116. usage()
  117. {
  118.     fprintf(stderr, "usage: %s [-dltv] [-b prefix] filename\n", myname);
  119.     exit(1);
  120. }
  121.  
  122.  
  123. getargs(argc, argv)
  124. int argc;
  125. char *argv[];
  126. {
  127.     register int i;
  128.     register char *s;
  129.  
  130.     if (argc > 0) myname = argv[0];
  131.     for (i = 1; i < argc; ++i)
  132.     {
  133.     s = argv[i];
  134.     if (*s != '-') break;
  135.     switch (*++s)
  136.     {
  137.     case '\0':
  138.         input_file = stdin;
  139.         if (i + 1 < argc) usage();
  140.         return;
  141.  
  142.     case '_':
  143.         ++i;
  144.         goto no_more_options;
  145.  
  146.     case 'b':
  147.         if (*++s || ++i >= argc) usage();
  148.         prefix = argv[i];
  149.         continue;
  150.  
  151.     case 'd':
  152.         dflag = 1;
  153.         break;
  154.  
  155.     case 'l':
  156.         lflag = 1;
  157.         break;
  158.  
  159.     case 't':
  160.         tflag = 1;
  161.         break;
  162.  
  163.     case 'v':
  164.         vflag = 1;
  165.         break;
  166.  
  167.     default:
  168.         usage();
  169.     }
  170.  
  171.     for (;;)
  172.     {
  173.         switch (*++s)
  174.         {
  175.         case '\0':
  176.         goto end_of_option;
  177.  
  178.         case 'd':
  179.         dflag = 1;
  180.         break;
  181.  
  182.         case 'l':
  183.         lflag = 1;
  184.         break;
  185.  
  186.         case 't':
  187.         tflag = 1;
  188.         break;
  189.  
  190.         case 'v':
  191.         vflag = 1;
  192.         break;
  193.  
  194.         default:
  195.         usage();
  196.         }
  197.     }
  198. end_of_option:;
  199.     }
  200.  
  201. no_more_options:;
  202.     if (i + 1 != argc) usage();
  203.     input_file_name = argv[i];
  204. }
  205.  
  206.  
  207. char *
  208. allocate(n)
  209. unsigned n;
  210. {
  211.     register char *p;
  212.  
  213.     p = calloc((unsigned) 1, n);
  214.     if (!p) no_space();
  215.     return (p);
  216. }
  217.  
  218.  
  219. create_file_names()
  220. {
  221.     int i, len;
  222.     char *tmpdir;
  223.  
  224.     tmpdir = getenv("TMPDIR");
  225. #ifdef os2
  226.     if (tmpdir == 0) 
  227.     {
  228.         tmpdir = ".";
  229.     pathsep = '\\';
  230.     }
  231. #else
  232. #ifdef vms
  233.     if (tmpdir == 0) 
  234.     {
  235.         tmpdir = "";
  236.     pathsep = '.';
  237.     }
  238. #else
  239.     if (tmpdir == 0) 
  240.     {
  241.         tmpdir = "/tmp";
  242.     pathsep = '/';
  243.     }
  244. #endif
  245. #endif
  246.  
  247.     len = strlen(tmpdir);
  248.     i = len + 13;
  249.     if (len && tmpdir[len-1] != pathsep)
  250.     ++i;
  251.  
  252.     action_file_name = MALLOC(i);
  253.     if (action_file_name == 0) no_space();
  254.     text_file_name = MALLOC(i);
  255.     if (text_file_name == 0) no_space();
  256.     union_file_name = MALLOC(i);
  257.     if (union_file_name == 0) no_space();
  258.  
  259.     strcpy(action_file_name, tmpdir);
  260.     strcpy(text_file_name, tmpdir);
  261.     strcpy(union_file_name, tmpdir);
  262.  
  263.     if (len && tmpdir[len - 1] != pathsep)
  264.     {
  265.     action_file_name[len] = pathsep;
  266.     text_file_name[len] = pathsep;
  267.     union_file_name[len] = pathsep;
  268.     ++len;
  269.     }
  270.  
  271.     strcpy(action_file_name + len, temp_form);
  272.     strcpy(text_file_name + len, temp_form);
  273.     strcpy(union_file_name + len, temp_form);
  274.  
  275.     action_file_name[len + 5] = 'a';
  276.     text_file_name[len + 5] = 't';
  277.     union_file_name[len + 5] = 'u';
  278.  
  279.     mktemp(action_file_name);
  280.     mktemp(text_file_name);
  281.     mktemp(union_file_name);
  282.  
  283.     len = strlen(prefix);
  284.     if (dflag)
  285.     {
  286.     /*  the number 7 below is the size of ".tab.h"; sizeof is not used  */
  287.     /*  because of a C compiler that thinks sizeof(".tab.h") == 6        */
  288.     defines_file_name = MALLOC(len + 7);
  289.     if (defines_file_name == 0) no_space();
  290.     strcpy(defines_file_name, prefix);
  291.     strcpy(defines_file_name + len, DEFINES_SUFFIX);
  292.     }
  293.  
  294.     output_file_name = MALLOC(len + 7);
  295.     if (output_file_name == 0) no_space();
  296.     strcpy(output_file_name, prefix);
  297.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  298.  
  299.     if (vflag)
  300.     {
  301.     verbose_file_name = MALLOC(len + 9);
  302.     if (verbose_file_name == 0) no_space();
  303.     strcpy(verbose_file_name, prefix);
  304.     strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  305.     }
  306. }
  307.  
  308.  
  309. open_files()
  310. {
  311.     create_file_names();
  312.  
  313.     if (input_file == 0)
  314.     {
  315.     input_file = fopen(input_file_name, "r");
  316.     if (input_file == 0) open_error(input_file_name);
  317.     }
  318.  
  319.     action_file = fopen(action_file_name, "w");
  320.     if (action_file == 0) open_error(action_file_name);
  321.  
  322.     text_file = fopen(text_file_name, "w");
  323.     if (text_file == 0) open_error(text_file_name);
  324.  
  325.     if (vflag)
  326.     {
  327.     verbose_file = fopen(verbose_file_name, "w");
  328.     if (verbose_file == 0) open_error(verbose_file_name);
  329.     }
  330.  
  331.     if (dflag)
  332.     {
  333.     defines_file = fopen(defines_file_name, "w");
  334.     if (defines_file == 0) open_error(defines_file_name);
  335.     union_file = fopen(union_file_name, "w");
  336.     if (union_file ==  0) open_error(union_file_name);
  337.     }
  338.  
  339.     output_file = fopen(output_file_name, "w");
  340.     if (output_file == 0) open_error(output_file_name);
  341. }
  342.  
  343.  
  344. int
  345. main(argc, argv)
  346. int argc;
  347. char *argv[];
  348. {
  349.     set_signals();
  350.     getargs(argc, argv);
  351.     open_files();
  352.     reader();
  353.     lr0();
  354.     lalr();
  355.     make_parser();
  356.     verbose();
  357.     output();
  358.     done(0);
  359.     /*NOTREACHED*/
  360. }
  361.